{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 线性回归代码实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "def loaddata():\n",
    "    data = np.loadtxt('data/data1.txt',delimiter=',')\n",
    "    n = data.shape[1]-1 #特征数\n",
    "    X = data[:,0:n]\n",
    "    y = data[:,-1].reshape(-1,1)\n",
    "    return X,y"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 特征归一化\n",
    "归一化有很多种方式，我们这里采取的方式是：对每一个特征，这列中的每个数据分别减去这列的均值，然后再除以这列的方差"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "def featureNormalize(X):\n",
    "    mu = np.average(X,axis=0)\n",
    "    sigma =np.std(X,axis=0,ddof=1)\n",
    "    X = (X-mu)/sigma\n",
    "    return X,mu,sigma"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 计算损失函数\n",
    "<img src=\"images/02.png\" width=\"300\" align=\"left\"/>\n",
    "<p>\n",
    "<img src=\"images/03.png\" width=\"300\" align=\"left\"/>"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "def computeCost(X,y,theta):\n",
    "    m = X.shape[0]\n",
    "    return np.sum(np.power(np.dot(X,theta)-y,2))/(2*m)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 梯度下降法求解\n",
    "<img src=\"images/01.png\" width=\"300\" align=\"left\"/>"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "def gradientDescent(X,y,theta,iterations,alpha):\n",
    "    c = np.ones(X.shape[0]).transpose()\n",
    "    X = np.insert(X,0,values=c,axis=1)#对原始数据加入一个全为1的列\n",
    "    m = X.shape[0]\n",
    "    n = X.shape[1]\n",
    "    costs = np.zeros(iterations)\n",
    "    for num in range(iterations):\n",
    "        for j in range(n):\n",
    "            theta[j] = theta[j]+(alpha/m)*np.sum(y-np.dot(X,theta)*X[:,j].reshape(-1,1))\n",
    "        costs[num] = computeCost(X,y,theta)\n",
    "    return theta,costs"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 带有L2正则化的线性回归求解（Ridge Regression）\n",
    "<img src=\"images/07.png\" width=\"300\" align=\"left\"/>"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "def gradientDescent_ridge(X,y,theta,iterations,alpha,lamda=0.02):\n",
    "    c = np.ones(X.shape[0]).transpose()\n",
    "    X = np.insert(X,0,values=c,axis=1)#对原始数据加入一个全为1的列\n",
    "    m = X.shape[0]\n",
    "    n = X.shape[1]\n",
    "    costs = np.zeros(iterations)\n",
    "    for num in range(iterations):\n",
    "        for j in range(n):\n",
    "            theta[j] = theta[j]+(alpha/m)*np.sum(y-np.dot(X,theta)*X[:,j].reshape(-1,1))-2*lamda*theta[j]\n",
    "        costs[num] = computeCost(X,y,theta)\n",
    "    return theta,costs"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 预测函数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "def predict(X):\n",
    "    X = (X-mu)/sigma\n",
    "    c = np.ones(X.shape[0]).transpose()\n",
    "    X = np.insert(X, 0, values=c, axis=1)\n",
    "    return np.dot(X,theta)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "X_orgin,y = loaddata()\n",
    "X,mu,sigma = featureNormalize(X_orgin)\n",
    "theta = np.zeros(X.shape[1]+1).reshape(-1,1)\n",
    "iterations = 400\n",
    "alpha = 0.01\n",
    "theta,costs = gradientDescent(X,y,theta,iterations,alpha)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 调用gradientDescent_ridge实现L2正则化\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "theta,costs = gradientDescent_ridge(X,y,theta,iterations,alpha,0.0005)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 画损失函数图"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "[<matplotlib.lines.Line2D at 0x24dd3d962b0>]"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAblklEQVR4nO3deXgc9Z3n8fe3u3Vbpy3bsixbPrGxDbaROeJwhtPJEwyTTIBniGdgQp5M2BzLTpZsdnaYmWeySYaQZHYDT2AhMLMcOzPAQAgTcAjE4TQyNka+D4RvST5kSZZ1df/2jy7ZwkjW4VZXV/fn9Tx6uvrXJfeHQv649KuqLnPOISIiwRPyO4CIiIyMClxEJKBU4CIiAaUCFxEJKBW4iEhARZL5ZuPGjXPV1dXJfEsRkcBbs2bNQedc+anjSS3w6upqamtrk/mWIiKBZ2Yf9TeuKRQRkYBSgYuIBJQKXEQkoFTgIiIBpQIXEQkoFbiISECpwEVEAioQBf7q5kYeeG2H3zFERFJKIAr8je0H+elvtxKN6bPLRUR6BaLA51QU0dkTo/7QMb+jiIikjGAU+MRCADbvb/U5iYhI6ghEgc8cP4ZwyNh8oMXvKCIiKSMQBZ6bFWbauAI2aQ9cROSEQBQ4xKdRtjRoD1xEpFdgCnxuRRG7Dx+ntaPb7ygiIikhMAXeeyBza4OmUUREIEAFfpZX4JoHFxGJG7TAzazKzF41s01mtsHMvumN32Nme81snfe1bDSDVpbkUZgT0ZkoIiKeodxSrQe4yzn3npkVAmvMbKX32k+cc/eOXryTzIw5FYU6F1xExDPoHrhzbr9z7j1vuRXYBFSOdrD+zJlYxJYDrTinS+pFRIY1B25m1cAi4B1v6E4zW29mj5hZaaLDnWpORSGtnT3sbT4+2m8lIpLyhlzgZjYGeBr4lnOuBXgAmAEsBPYDPx7g++4ws1ozq21qajqjsHMmFgG6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},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x_axis = np.linspace(1,iterations,iterations)\n",
    "plt.plot(x_axis,costs[0:iterations])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 画数据散点图和求得的直线"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "[<matplotlib.lines.Line2D at 0x24dd3e01880>]"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAdeUlEQVR4nO3de3RU1b0H8O+PGCWAEpGoGIEgVSw+gJpSEWupWkHRJYqKtLW9S1vaW+1V6+LeoFbRakmLz1WtFatLvVqLrYhcUREBXyjaIChQQBEjJVBBTXyQYF6/+0dmwpyZc2bOzJzHPjPfz1ouyU4yZzMr+Z7NPvu3t6gqiIgoenqE3QEiIsoNA5yIKKIY4EREEcUAJyKKKAY4EVFE7RXkxfr3769VVVVBXpKIKPJWrlz5sapWJLcHGuBVVVWoq6sL8pJERJEnIh/atXMKhYgoojIGuIgMFJFlIrJeRNaJyOWx9pki0iAiq2P/neF/d4mIKM7NFEo7gKtU9S0R2RfAShFZHPvc7ap6i3/dIyIiJxkDXFW3A9ge+/MXIrIeQKXfHSMiovSymgMXkSoAowC8EWu6TETeEZEHRGR/h++ZJiJ1IlK3c+fO/HpLRETdXAe4iPQB8ASAK1T1cwD3ABgKYCS6Rui32n2fqs5R1WpVra6oSFk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2qVgd+YYPxPbHi+5GK74kV3w9OoRARRRYDnIgoosIK8DkhXddkfE+s+H6k4ntiVfTvRyhz4ERElD9OoRARRRQDnIgookILcBE5X0TWiUiniBTtUiARmSAiG0Vkk4jUhN2fsInIAyKyQ0TWht0XE4jIQBFZJiLrY78vl4fdp7CJSE8ReVNE3o69JzeE3aewhDkCXwvgXAAvh9iHUIlICYC7AZwOYDiAqSIyPNxehe5BABPC7oRB2gFcpapfB3A8gEv5M4KvAJysqiMAjAQwQUSOD7dL4QgtwFV1vaoW++nEowFsUtXNqtoK4K8Azg65T6FS1ZcBfBp2P0yhqttV9a3Yn78AsB5AUW+Qr12+jH1YGvuvKFdjcA48XJUA/pXw8VYU+S8nORORKgCjALwRcldCJyIlIrIawA4Ai1W1KN8TX0/kEZEXABxs86lrVPUpP68dEWLTVpQjCUpPRPoAeALAFar6edj9CZuqdgAYKSLlAJ4UkaNVteiem/ga4Kp6qp+vXwC2AhiY8PGhALaF1BcylIiUoiu8H1XVeWH3xySq2iQiL6LruUnRBTinUML1DwCHi8gQEdkbwIUAFoTcJzKIiAiA+wGsV9Xbwu6PCUSkIjbyhoiUATgVwIZQOxWSMJcRniMiWwGMAbBQRBaF1ZewqGo7gMsALELXw6nHVXVduL0Kl4g8BuB1AMNEZKuIXBJ2n0I2FsBFAE4WkdWx/84Iu1MhGwBgmYi8g65B0GJVfTrkPoWCpfRERBHFKRQioohigBMRRRQDnIgoohjgREQRxQAnIoooBjgRUUQxwImIIur/ATmBGp4KR27VAAAAAElFTkSuQmCC\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X,y)\n",
    "h_theta = theta[0]+theta[1]*X\n",
    "plt.plot(X,h_theta)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 预测数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.92378006]]\n"
     ]
    }
   ],
   "source": [
    "print(predict([[5.5277]]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 常见的模型评价指标"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 1、均方误差\n",
    "<img src=\"images/04.png\" width=\"200\" align=\"left\"/>"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "y_true = np.array([1,2,3,4,5])\n",
    "y_pred = np.array([1.1,2.2,3.1,4.2,5])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "0.020000000000000035"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def mse(y_true,y_pred):\n",
    "    return np.sum(np.power(y_true-y_pred,2))/len(y_true)\n",
    "mse(y_true,y_pred)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 2、均方根误差\n",
    "<img src=\"images/05.png\" width=\"200\" align=\"left\"/>"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "0.14142135623730964"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def rmse(y_true,y_pred):\n",
    "    return np.sqrt(np.sum(np.power(y_true-y_pred,2))/len(y_true))\n",
    "rmse(y_true,y_pred)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "##### 3、平均绝对误差\n",
    "<img src=\"images/06.png\" width=\"200\" align=\"left\"/>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "0.1200000000000001"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def mae(y_true,y_pred):\n",
    "    return np.sum(np.abs(y_true-y_pred))/len(y_true)\n",
    "mae(y_true,y_pred)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 对模型进行评价"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1.365577  ],\n       [ 0.92378006],\n       [ 3.1926167 ],\n       [ 2.04306471],\n       [ 1.17570445],\n       [ 3.08967741],\n       [ 2.40202472],\n       [ 3.2377522 ],\n       [ 1.6508789 ],\n       [ 0.56489591],\n       [ 1.06260018],\n       [ 7.47510369],\n       [ 1.0802751 ],\n       [ 3.10902119],\n       [ 1.00949959],\n       [ 0.81128266],\n       [ 1.55924245],\n       [ 0.62216869],\n       [ 1.60794328],\n       [ 2.09434471],\n       [ 1.42550481],\n       [12.10699259],\n       [ 0.89525746],\n       [ 1.52943026],\n       [ 0.95199923],\n       [11.10187422],\n       [ 6.46164094],\n       [ 5.04233794],\n       [ 6.72562675],\n       [13.57332755],\n       [ 0.71494301],\n       [ 1.72916435],\n       [ 3.7460766 ],\n       [ 1.19997901],\n       [ 2.95935338],\n       [ 2.74869574],\n       [ 2.87196497],\n       [ 0.98340444],\n       [ 6.46770958],\n       [ 1.55013949],\n       [ 0.83214361],\n       [ 1.95150412],\n       [ 5.61203145],\n       [ 1.11039072],\n       [ 2.6662381 ],\n       [ 2.11126105],\n       [ 0.57672976],\n       [ 1.13140338],\n       [ 5.60596281],\n       [ 0.93432432],\n       [ 2.45042212],\n       [ 0.75689248],\n       [ 2.36219928],\n       [ 2.4981368 ],\n       [ 1.53451274],\n       [ 1.55431168],\n       [ 1.49005996],\n       [ 1.00874101],\n       [ 3.79310855],\n       [ 3.90188891],\n       [ 3.42534901],\n       [ 0.65949082],\n       [12.87239972],\n       [ 8.03948714],\n       [11.11249434],\n       [ 2.20615939],\n       [ 3.02307409],\n       [ 4.49540183],\n       [ 0.90231225],\n       [12.16085176],\n       [ 4.41954384],\n       [ 2.29438223],\n       [ 1.28676055],\n       [ 2.21200046],\n       [ 0.54388325],\n       [ 1.69768328],\n       [ 2.44920839],\n       [ 0.55116561],\n       [ 4.52422786],\n       [ 0.6051765 ],\n       [ 1.07663391],\n       [ 0.6663939 ],\n       [ 1.55188422],\n       [ 4.14091744],\n       [ 1.67340872],\n       [ 3.19155469],\n       [ 3.69449316],\n       [ 1.28357451],\n       [ 0.91824243],\n       [ 0.56853709],\n       [ 1.06032445],\n       [ 2.52354922],\n       [ 1.18397297],\n       [ 0.75514775],\n       [ 3.0217845 ],\n       [ 6.89099717],\n       [ 0.85490101]])"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred = predict(X_orgin)\n",
    "y_pred"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[17.592  ],\n       [ 9.1302 ],\n       [13.662  ],\n       [11.854  ],\n       [ 6.8233 ],\n       [11.886  ],\n       [ 4.3483 ],\n       [12.     ],\n       [ 6.5987 ],\n       [ 3.8166 ],\n       [ 3.2522 ],\n       [15.505  ],\n       [ 3.1551 ],\n       [ 7.2258 ],\n       [ 0.71618],\n       [ 3.5129 ],\n       [ 5.3048 ],\n       [ 0.56077],\n       [ 3.6518 ],\n       [ 5.3893 ],\n       [ 3.1386 ],\n       [21.767  ],\n       [ 4.263  ],\n       [ 5.1875 ],\n       [ 3.0825 ],\n       [22.638  ],\n       [13.501  ],\n       [ 7.0467 ],\n       [14.692  ],\n       [24.147  ],\n       [-1.22   ],\n       [ 5.9966 ],\n       [12.134  ],\n       [ 1.8495 ],\n       [ 6.5426 ],\n       [ 4.5623 ],\n       [ 4.1164 ],\n       [ 3.3928 ],\n       [10.117  ],\n       [ 5.4974 ],\n       [ 0.55657],\n       [ 3.9115 ],\n       [ 5.3854 ],\n       [ 2.4406 ],\n       [ 6.7318 ],\n       [ 1.0463 ],\n       [ 5.1337 ],\n       [ 1.844  ],\n       [ 8.0043 ],\n       [ 1.0179 ],\n       [ 6.7504 ],\n       [ 1.8396 ],\n       [ 4.2885 ],\n       [ 4.9981 ],\n       [ 1.4233 ],\n       [-1.4211 ],\n       [ 2.4756 ],\n       [ 4.6042 ],\n       [ 3.9624 ],\n       [ 5.4141 ],\n       [ 5.1694 ],\n       [-0.74279],\n       [17.929  ],\n       [12.054  ],\n       [17.054  ],\n       [ 4.8852 ],\n       [ 5.7442 ],\n       [ 7.7754 ],\n       [ 1.0173 ],\n       [20.992  ],\n       [ 6.6799 ],\n       [ 4.0259 ],\n       [ 1.2784 ],\n       [ 3.3411 ],\n       [-2.6807 ],\n       [ 0.29678],\n       [ 3.8845 ],\n       [ 5.7014 ],\n       [ 6.7526 ],\n       [ 2.0576 ],\n       [ 0.47953],\n       [ 0.20421],\n       [ 0.67861],\n       [ 7.5435 ],\n       [ 5.3436 ],\n       [ 4.2415 ],\n       [ 6.7981 ],\n       [ 0.92695],\n       [ 0.152  ],\n       [ 2.8214 ],\n       [ 1.8451 ],\n       [ 4.2959 ],\n       [ 7.2029 ],\n       [ 1.9869 ],\n       [ 0.14454],\n       [ 9.0551 ],\n       [ 0.61705]])"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mse 20.27031293808692\n",
      "rmse 4.502256427402478\n",
      "mae 3.3135443315357453\n"
     ]
    }
   ],
   "source": [
    "print('mse',mse(y,y_pred))\n",
    "print('rmse',rmse(y,y_pred))\n",
    "print('mae',mae(y,y_pred))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}